package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.TcpResponseStatus;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVCNTChangeLink;

/**
 * @className: CNTChangeLinkPS
 * @describtion: CNTChangeLinkPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 19:19
 */
public class CNTChangeLinkPS {

    private IVCNTChangeLink ivcntChangeLink;

    public CNTChangeLinkPS(IVCNTChangeLink ivcntChangeLink) {
        this.ivcntChangeLink = ivcntChangeLink;
    }

    /**
     * 提交修改
     */
    public void submitChangeLink(){
        String ssid = ivcntChangeLink.getLinkSSID();
        String password = ivcntChangeLink.getLinkPassword();
        String confirmPwd = ivcntChangeLink.getLinkConfirmPassword();
        if(ssid == null || TextUtils.isEmpty(ssid)){
            ivcntChangeLink.toastMsg("SSID不能为空");
            return;
        }
        if(password == null || TextUtils.isEmpty(password)
            || confirmPwd == null || TextUtils.isEmpty(confirmPwd)){
            ivcntChangeLink.toastMsg("密码不能为空！");
            return;
        }else if(password.length() < 8 || confirmPwd.length() < 8){
            ivcntChangeLink.toastMsg("密码不能少于8位！");
            return;
        }else if(!password.equals(confirmPwd)){
            ivcntChangeLink.toastMsg("两次输入的密码不一致");
            return;
        }

        //  跟Link通信发送新的SSID和密码，请求修改Link
        GlobalField.businessTcp.modifyDeviceWifi(GlobalField.userSession.getUserId(), ssid, password, new CallbackListener<TcpResponseStatus>() {
            @Override
            public void onSuccess(TcpResponseStatus result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivcntChangeLink.changLinkSuccess();
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivcntChangeLink.toastMsg(msg);
                    }
                });
            }
        });
    }
}
